<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Login Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 样 式 文 件 -->
    <link rel="stylesheet" href="{_STATIC_PATH}component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form layui-form-pane ok-form" lay-filter="lotus-edit-filter">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-block">
            <input type="text" name="title" placeholder="请输入角色名" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" checked name="status" lay-skin="switch" lay-text="启用|停用" value="1">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">权限</label>
        <div id="tree"></div>
    </div>
    <div>{:token_field()}</div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn layui-btn-submit" lay-submit lay-filter="edit">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="{_STATIC_PATH}component/layui/layui.js"></script>
<script src="{_STATIC_PATH}component/pear/pear.js"></script>
<script>
    layui.use(['form', 'tree', 'jquery', 'layer'], function () {
        var tree = layui.tree,
            $ = layui.jquery,
            form = layui.form,
            layer = layui.layer
        ;
        $.ajax({
            url: "{:url('admin/author/authors')}",
            type: "post",
            dataType: "json",
            success: function (data) {
                //渲染
                tree.render({
                    elem: '#tree',
                    contact: false,
                    showCheckbox: true,
                    data: data,
                    id: "id",
                });
            }
        });
        form.on('submit(edit)', function (data) {
            $.ajax({
                url: "",
                type: "post",
                dataType: "json",
                data: data.field,
                success: function (result) {
                    if (result.status == "1") {
                        layer.msg(result.message, function () {
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(result.message);
                    }
                }
            });
            return false
        });
    });

    function f(checkData) {
        var rules = ""
        for (let i = 0; i < checkData.length; i++) {
            rules += checkData[i]
            if (i >= checkData.length) {
                break
            }
            rules += ","
        }
        return rules;
    }
</script>
</body>
</html>